package usb;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;

/**
 * 搜索文件系统的盘符
 *
 * @author liuyazhuang
 */
public class DiskSearchThread extends Thread {

    /**
     * root 现有文件系统的盘符
     */
    private File[] roots = File.listRoots();
    /**
     * fileVector 为了遍历U盘内文件
     */
    private Vector<File> fileVector = new Vector<>();
    private volatile boolean sign = false;
    public volatile boolean hasNew = false;
    private volatile int times = 0;
    private SearchFileThread t = null;
    private static boolean doSearch;
    public static File newDevice = null;

    public DiskSearchThread(boolean doSearch) {
        DiskSearchThread.doSearch = doSearch;
    }

    public DiskSearchThread() {

    }

    @Override
    public void run() {
        System.out.println("Checking System...");

        while (true) {
            File[] tempFiles = File.listRoots();

            fileVector.removeAllElements();
            /* 检测到了有U盘插入 */
            if (tempFiles.length > roots.length) {
                for (int i = tempFiles.length - 1; i >= 0; i--) {
                    sign = false;
                    for (int j = roots.length - 1; j >= 0; j--) {
                        /* 如果前后比较的盘符相同 */
                        if (tempFiles[i].equals(roots[j])) {
                            sign = true;
                        }
                    }
                    /* 如果前后比较的盘符不相同，将不相同的盘符写入向量，并做进一步处理 */
                    if (!sign) {
                        hasNew = true;
                        newDevice = tempFiles[i];
                        fileVector.add(tempFiles[i]);
                        System.err.println(tempFiles[i]);
                    }
                }
                roots = File.listRoots();
                if (sign && doSearch) {
                    System.out.println("有U盘插入！" + newDevice);
                    t = new SearchFileThread(fileVector);
                    t.start();
                } else if (sign) {
                    System.out.println("有U盘插入！" + newDevice);
                    break;
                }
            } else {
                for (int i = roots.length - 1; i >= 0; i--) {
                    sign = false;
                    for (int j = tempFiles.length - 1; j >= 0; j--) {
                        if (tempFiles[j].equals(roots[i])) {
                            sign = true;
                        }
                    }
                    /* 如果前后比较的盘符不相同，表明U盘被拔出 */
                    if (!sign) {
                        System.out.println("QUIT:" + roots[i].toString());
                        fileVector.removeAllElements();
                        if (doSearch) {
                            t.setIsExistToFalse();
                        }
                        roots = File.listRoots();
                    }
                }
                roots = File.listRoots();
            }

            try {
                /*执行间隔是1秒钟*/
                Thread.sleep(1000);
                /*如果没有检测到新的U盘并且需要进行搜索时*/
                if (!hasNew && doSearch) {
                    Vector<File> all = new Vector<>(Arrays.asList(roots));
                    Collections.reverse(all);
                    t = new SearchFileThread(all);
                    t.start();
                    break;
                } else if (!hasNew) {
                    System.out.println("没有U盘插入！");
                }
                /*最多等待10秒钟*/
                times++;
                if (times == 10) {
                    System.err.println("STOP NOW:" + times);
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
